﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Wss.DragRaycast
{

    /// <summary>
    /// UI 从 UI层拖拽出东西到 World 中,发射一条射线到世界中
    /// </summary>
    [AddComponentMenu("DragRaycast/UIDragRaycast")]
    public class UIDragRaycast : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
    {
        /// <summary>
        /// 需要拖拽移动的图标
        /// </summary>
        public GameObject mIcon;

        /// <summary>
        /// 拖拽的偏移值
        /// </summary>
        /// <returns></returns>
        public Vector2 offset = new Vector2(0, 0);

        [HideInInspector]
        public bool isDragging = false;

        public List<string> emitTags = new List<string>();

        private RaycastHit _lastRaycastHit = new RaycastHit();

        private void Awake()
        {
            if (mIcon == null)
            {
                var obj = new GameObject();
                obj.transform.SetParent(this.transform);
                mIcon = obj;
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            this.isDragging = true;
        }

        public void OnDrag(PointerEventData eventData)
        {
            this.isDragging = true;
            mIcon.transform.position = eventData.position + offset;
        }

        private void Update()
        {
            if (this.isDragging)
            {
                Ray ray = Camera.main.ScreenPointToRay(mIcon.transform.position);
                RaycastHit hit = _lastRaycastHit;
                if (Physics.Raycast(ray, out hit))
                {
                    Debug.DrawLine(ray.origin, hit.point, Color.red);
                    this.OnRaycastHit(hit);
                }
                else
                {
                    this.OnRaycastHitCancel();
                    Debug.DrawRay(ray.origin, ray.direction, Color.green);
                }
            }
        }


        public void OnEndDrag(PointerEventData eventData)
        {
            this.isDragging = false;
            mIcon.transform.position = eventData.position + offset;
            Ray ray = Camera.main.ScreenPointToRay(mIcon.transform.position);
            RaycastHit hit = _lastRaycastHit;
            if (Physics.Raycast(ray, out hit))
            {
                Debug.DrawLine(ray.origin, hit.point, Color.red, 0.3f);
                this.OnRaycastHit(hit);
            }
            else
            {
                this.OnRaycastHitCancel();
            }

            mIcon.transform.localPosition = Vector3.zero;

        }


        /// <summary>
        /// 射线击中了目标
        /// </summary>
        /// <param name="hit"></param>
        public void OnRaycastHit(RaycastHit hit)
        {
            //射线击中的方法
            if (DragRaycastManager.Instance != null) DragRaycastManager.Instance.OnRaycastHit(hit);

            //查看是不是对应的接收器
            IDragRaycastReceiver receiver = hit.transform.GetComponent<IDragRaycastReceiver>();
            if (receiver == null) return;
            string tag = receiver.GetReceiveTag();

            if (receiver != null)
            {
                if (tag == "" || emitTags.Contains(tag))
                {
                    receiver.OnHitRayCast(hit);
                }
            }

        }

        /// <summary>
        /// 射线没击中目标（取消击中)
        /// </summary>
        /// <param name="hit"></param>
        public void OnRaycastHitCancel()
        {
            //实现射线取消的方法
        }


    }

}
